package com.itheima.dao.impl;

import com.itheima.dao.OrderDao;
import com.itheima.model.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * @Author：shenkunlin
 * @date： 2021/7/5 11:59
 * @Description：
 ***/
@Repository
public class OrderDaoImpl implements OrderDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /****
     * 下单
     * @param orderInfo
     * @return
     */
    @Override
    public int save(OrderInfo orderInfo) {
        //时间
        orderInfo.setCreateTime(new Date());
        //sql
        String sql = "insert into order_info(count,price,create_time,item_id) values(?,?,?,?)";
        //增加
        return jdbcTemplate.update(sql,orderInfo.getCount(),orderInfo.getPrice(),orderInfo.getCreateTime(),orderInfo.getItemId());
    }

    /**
     * 根据ID查询订单信息
     * @param id
     * @return
     */
    @Override
    public OrderInfo one(Integer id) {
        //sql
        String sql = "select * from order_info where id=?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<OrderInfo>(OrderInfo.class),id);
    }
}
